package com.example.gesturedemo;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.yiutil.tools.Logger;

public class MainActivity extends FragmentActivity{

    private PageView view_pager;
    private PagerAdapter adapter;
    int nextIndex;

    private final int[] Colors = { 0xff03a9f4, 0xffcddc39, 0xffe91e63, 0xff00bcd4, 0xfff5722, };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        view_pager = (PageView) findViewById(R.id.view_pager);
        view_pager.setOnPageChangeListener(new OnPageChangeListener() {

            int mPostion = -1;
            ColorDrawable colorDrawable = new ColorDrawable();
            float tmpOffset;

            @Override
            public void onPageScrollStateChanged(int arg0) {
                Logger.ww("status:%d mPostion:%d", arg0, mPostion);
                if (arg0 == 0) {
                    mPostion = view_pager.getCurrentItem();
                } 
            }

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                // Logger.dd("position:%d mPostion:%d,nextIndex:%d", position, mPostion, nextIndex);
                Logger.dd("positionOffset:%f positionOffsetPixels:%d", positionOffset, positionOffsetPixels);
                if (mPostion < 0) {
                    mPostion = 0;
                }

                int currentColor = Colors[mPostion];
                int nextColor = Colors[mPostion];
                if (tmpOffset != positionOffset) {
                    if (tmpOffset < positionOffset) {
                        Logger.dd("←左");
                        if (mPostion < 4) {
                            nextColor = Colors[mPostion + 1];
                        }
                        int blendColors = blendColors(currentColor, nextColor, positionOffset);
                        view_pager.setBackgroundColor(blendColors);
                    } else if (tmpOffset > positionOffset) {
                        Logger.dd("右→");
                        if (mPostion > 0) {
                            nextColor = Colors[mPostion - 1];
                        }
                        int blendColors = blendColors(currentColor, nextColor, positionOffset);
                        view_pager.setBackgroundColor(blendColors);
                    }
                } else {
                    Logger.dd("没动");
                }
                tmpOffset = positionOffset;
                if (positionOffsetPixels > 1) {
                    // getSupportActionBar().setBackgroundDrawable(colorDrawable);
                } else {
                    colorDrawable.setColor(Colors[mPostion]);
                    colorDrawable.setCallback(null);
                    getActionBar().setBackgroundDrawable(colorDrawable);
                }
            }

            @Override
            public void onPageSelected(int arg0) {
                // TODO Auto-generated method stub
                Logger.ii("arg0:%d mPostion:%d", arg0, mPostion);
                mPostion = arg0;

            }
        });

        adapter = new FragmentPagerAdapter(getSupportFragmentManager()) {


            @Override
            public Fragment getItem(int arg0) {
                nextIndex = arg0;
                MyFragment fragment = new MyFragment();
                Bundle bundle = new Bundle();
                bundle.putInt("INDEX",arg0);
				fragment.setArguments(bundle);
                return fragment;
            }

            @Override
            public int getCount() {
                return 5;
            }


        };
        view_pager.setAdapter(adapter);
    }


    private static int blendColors(int color1, int color2, float ratio) {
        final float inverseRation = 1f - ratio;
        float r = (Color.red(color1) * ratio) + (Color.red(color2) * inverseRation);
        float g = (Color.green(color1) * ratio) + (Color.green(color2) * inverseRation);
        float b = (Color.blue(color1) * ratio) + (Color.blue(color2) * inverseRation);
        return Color.rgb((int) r, (int) g, (int) b);
    }

    public static class MyFragment extends Fragment {

        int index;

        public MyFragment() {
            super();
        }
        
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        	index = getArguments().getInt("INDEX");
            TextView text = new TextView(inflater.getContext());
            text.setGravity(Gravity.CENTER);
            text.setTextColor(0xffffffff);
            text.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
            text.setText(getString(R.string.hello_world) + " " + index);
            return text;
        }
    }

}